added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CppDllCOMServer / ClassFactory.cpp
blob445aef0dc0536e058263f98c0c8a90aafb902bcd
1 /****************************** Module Header ******************************\
2 Module Name: ClassFactory.cpp
3 Project: CppDllCOMServer
4 Copyright (c) Microsoft Corporation.
6 The file implements the class factory for the SimpleObject COM class. A class
7 factory (aka a class object) is a component whose main purpose is to create
8 other components. It provides many controls over the creation process of the
9 component. When a client uses a CLSID to request the creation of an object
10 instance, the first step is creation of a class factory, an intermediate
11 object that contains an implementation of the methods of the IClassFactory
12 interface. While COM provides several instance creation functions, the first
13 step in the implementation of these functions is always the creation of a
14 class factory. For example, CoCreateInstance provides a wrapper over a
15 CoGetClassObject and CreateInstance method of IClassFactory interface.
16 CoCreateInstance internally creates class factory for the specified CLSID,
17 gets the IClassFactory interface pointer, and then creates the component by
18 calling CreateInstance on IClassFactory interface pointer and then returns
19 the requested interface pointer to the client by calling QueryInterface
20 method in the CreateInstance method of IClassFactory.
22 The class factory implements the IClassFactory interface. The class factory
23 object must implement these two methods of IClassFactory to support the
24 object creation.
26 IFACEMETHODIMP CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppv);
27 IFACEMETHODIMP LockServer(BOOL fLock);
29 This source is subject to the Microsoft Public License.
30 See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
31 All other rights reserved.
33 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
34 EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
36 \***************************************************************************/
38 #include "ClassFactory.h"
39 #include "SimpleObject.h"
42 extern long g_cDllRef;
45 ClassFactory::ClassFactory() : m_cRef(1)
47 InterlockedIncrement(&g_cDllRef);
50 ClassFactory::~ClassFactory()
52 InterlockedDecrement(&g_cDllRef);
57 // IUnknown
60 IFACEMETHODIMP ClassFactory::QueryInterface(REFIID riid, void **ppv)
62 HRESULT hr = S_OK;
64 if (IsEqualIID(IID_IUnknown, riid) ||
65 IsEqualIID(IID_IDispatch, riid) || // For implementing IDispatch
66 IsEqualIID(IID_IClassFactory, riid))
68 *ppv = static_cast<IUnknown *>(this);
69 AddRef();
71 else
73 hr = E_NOINTERFACE;
74 *ppv = NULL;
77 return hr;
80 IFACEMETHODIMP_(ULONG) ClassFactory::AddRef()
82 return InterlockedIncrement(&m_cRef);
85 IFACEMETHODIMP_(ULONG) ClassFactory::Release()
87 ULONG cRef = InterlockedDecrement(&m_cRef);
88 if (0 == cRef)
90 delete this;
92 return cRef;
96 //
97 // IClassFactory
100 IFACEMETHODIMP ClassFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppv)
102 HRESULT hr = CLASS_E_NOAGGREGATION;
104 // pUnkOuter is used for aggregation. We do not support it in the sample.
105 if (pUnkOuter == NULL)
107 hr = E_OUTOFMEMORY;
109 // Create the COM component.
110 SimpleObject *pSimpleObj = new SimpleObject();
111 if (pSimpleObj)
113 // Query the specified interface.
114 hr = pSimpleObj->QueryInterface(riid, ppv);
115 pSimpleObj->Release();
119 return hr;
122 IFACEMETHODIMP ClassFactory::LockServer(BOOL fLock)
124 if (fLock)
126 InterlockedIncrement(&g_cDllRef);
128 else
130 InterlockedDecrement(&g_cDllRef);
132 return S_OK;